home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / label_date.pro < prev    next >
Text File  |  1997-07-08  |  5KB  |  139 lines

  1. ; $Id: label_date.pro,v 1.5 1997/03/11 22:24:47 dave Exp $
  2. ;
  3. ; Copyright (c) 1993-1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5.  
  6. FUNCTION LABEL_DATE, axis, index, x, DATE_FORMAT = format, MONTHS = months, $
  7.               OFFSET= offs
  8. ;+
  9. ; NAME:
  10. ;    LABEL_DATE
  11. ;
  12. ; PURPOSE:
  13. ;    This function labels axes with dates and times.
  14. ;
  15. ; CATEGORY:
  16. ;    Plotting.
  17. ;
  18. ; CALLING SEQUENCE:
  19. ;    To set up:
  20. ;        dummy = LABEL_DATE(DATE_FORMAT='string')
  21. ;    To use:
  22. ;        PLOT, x, y, XTICKFORMAT='LABEL_DATE'
  23. ;
  24. ; INPUTS:
  25. ;    No explicit user defined inputs. When called from the plotting
  26. ;    routines, the input parameters are (Axis, Index, Value)
  27. ;
  28. ; KEYWORD PARAMETERS:
  29. ;    DATE_FORMAT: a format string which may contain the following:
  30. ;               %M for month (3 character abbr)
  31. ;               %N for month (2 digit abbr)
  32. ;               %D for day of month,
  33. ;               %Y for 4 digit year.
  34. ;               %Z for last two digits of year.
  35. ;         For time:
  36. ;               %H for Hours, 2 digits.
  37. ;               %I for mInutes, 2 digits.
  38. ;               %S for Seconds, 2 digits.
  39. ;               %% is %.
  40. ;             Other characters are passed directly thru.
  41. ;             For example, '%M %D, %Y' prints DEC 11, 1993
  42. ;               '%M %2Y' yields DEC 93
  43. ;               '%D-%M' yields 11-DEC
  44. ;               '%D/%N/%Y' yields 11/12/1993
  45. ;               '%M!C%Y' yields DEC on the top line, 1993 on
  46. ;               the bottom (!C is the new line graphic command).
  47. ;
  48. ;    MONTHS:      The names of the months, a twelve element string array.
  49. ;             If omitted, use Jan, Feb, ..., Dec.
  50. ;
  51. ;    OFFSET:         An optional starting offset of the plot.
  52. ;        Unfortunately, single precision floating point is not accurate
  53. ;        enough to properly represent Julian times.  This offset, which
  54. ;        may be double precision, contains an offset that is added to
  55. ;        all x values, before conversion to Julian date and time.
  56.  
  57. ; OUTPUTS:
  58. ;    The date string to be plotted.
  59. ;
  60. ; COMMON BLOCKS:
  61. ;    LABEL_DATE_COM.
  62. ;
  63. ; RESTRICTIONS:
  64. ;    Only one date axis may be simultaneously active.
  65. ;
  66. ; PROCEDURE:
  67. ;    Straightforward.
  68. ;
  69. ; EXAMPLE:
  70. ;    For example, to plot from Jan 1, 1993, to July 12, 1994:
  71. ;      Start_date = julday(1, 1, 1993)
  72. ;      End_date = julday(7, 12, 1994)
  73. ;      Dummy = LABEL_DATE(DATE_FORMAT='%N/%D')  ;Simple mm/dd
  74. ;      x = findgen(end_date+1 - start_date) + start_date ;Time axis
  75. ;      PLOT, x, sqrt(x), XTICKFORMAT = 'LABEL_DATE', XSTYLE=1
  76. ;      (Plot with X axis style set to exact.)
  77. ;    
  78. ; Example with times:
  79. ;    For example, to plot from 3PM, Jan 1, 1993, to 5AM, Jan 3,
  80. ;    1993:
  81. ;    Start_date = Julday(1,1,1993)   ;Also starting offset
  82. ;    Start_time = (3+12)/24.         ;Starting_time less offset
  83. ;    End_time = (Julday(1,3,1993) - Start_date) + 5./24. ;Ending
  84. ;           ;date/time - offset, note that the order of operations is
  85. ;               ; important to avoid loss of precision.
  86. ;    Dummy = LABEL_DATE(DATE_FORMAT='%D %M!C%H:%I', $
  87. ;        offset=Start_date)       ;MMM NN <new line> HH:MM format
  88. ;    x = findgen(20) * (End_time - Start_time) / 19 + start_time ;Time axis
  89. ;    PLOT, x, sqrt(x), XTICKFORMAT = 'LABEL_DATE', XSTYLE=1
  90. ;
  91. ; MODIFICATION HISTORY:
  92. ;    DMS, RSI.    April, 1993.    Written.
  93. ;    DMS, RSI.    March, 1997.    Added Time format.
  94. ;-
  95.  
  96. COMMON label_date_com, fmt, month_chr, offset
  97.  
  98. if keyword_set(format) then begin ;Save format string?
  99.     if n_elements(offs) ne 0 then offset = double(offs) else offset = 0.0d0
  100.     if keyword_set(months) then month_chr = months $
  101.     else month_chr = ['Jan','Feb','Mar', 'Apr', 'May', 'Jun', 'Jul', $
  102.                       'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
  103.     fmt = format
  104.     return, 0
  105. endif
  106.  
  107. if n_elements(month_chr) ne 12 or n_elements(fmt) le 0 $
  108.   or n_elements(offset) eq 0 then $
  109.   message,' Not initialized.'
  110.  
  111. x1 = x + offset
  112. caldat, long(x1), month, day, year ;Get the calendar date from julian
  113. frac = x1 - long(x1)            ;time of day, from 0 to 1.
  114.  
  115. n = strlen(fmt)
  116. out = ''
  117.  
  118. for i=0, n-1 do begin           ;Each format character...
  119.     c = strmid(fmt, i, 1)       ;The character.
  120.     if c eq '%' then begin
  121.         i = i + 1
  122.         c = strmid(fmt, i, 1)   ;The function
  123.         case c of        ;format character?
  124.             'M' : out = out + month_chr[month-1]
  125.             'N' : out = out + string(format='(i2.2)', month)
  126.             'D' : out = out + string(format='(i2.2)', day)
  127.             'Y' : out = out + string(format='(i4)', year)
  128.             'Z' : out = out + string(format='(i2.2)', year  mod 100)
  129.             'H' : out = out + string(format='(i2.2)', floor(24*frac))
  130.             'I' : out = out + string(format='(i2.2)', floor(1440 * frac mod 60))
  131.             'S' : out = out + string(format='(i2.2)', 86400L * frac mod 60)
  132.             '%' : out = out + '%'
  133.             else : message, 'Illegal character in date format string: '+fmt
  134.         endcase
  135.     endif else out = out + c
  136. endfor
  137. return, out
  138. end
  139.